package com.beerq.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.beerq.R;
import com.beerq.util.LogX;
import com.beerq.util.UtilsStyle;
import com.beerq.view.dialog.LoadingDialog;
import com.meiqia.core.callback.OnInitCallback;
import com.meiqia.meiqiasdk.util.MQConfig;


/**
 * 作者：minj on 2016/4/15 14:07
 */
public class BaseActivity extends Activity implements View.OnClickListener{

    protected String TAG = "BeerQ-BaseActivity";

    protected Context mContext;

    private ImageView imgLeft;

    private int defaultLayoutId= R.layout.tem_mainlayout;

    protected int statusBarType=0;//0是黑色 1是白色

//    private Toast mToast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mContext=this;

        UtilsStyle.setTranslateStatusBar(this,statusBarType);

        super.onCreate(savedInstanceState);
        LogX.trace(TAG, getClassName() + " - onCreate");
        setContentView(defaultLayoutId);

        imgLeft=(ImageView)findViewById(R.id.img_left);

        imgLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });



    }


    protected void setRightImage(int drawable,View.OnClickListener onClickListener){
        ImageView imageView=(ImageView)findViewById(R.id.img_right);
        imageView.setImageDrawable(getResources().getDrawable(drawable));
        imageView.setVisibility(View.VISIBLE);
        if(onClickListener!=null){
            imageView.setOnClickListener(onClickListener);
        }
    }


    protected void setRightTitle(String str){
        TextView txtView=(TextView)findViewById(R.id.txt_right_title);
        txtView.setText(str);
    }

    protected void setRightTitle(String str, View.OnClickListener clickListener){
        TextView txtView=(TextView)findViewById(R.id.txt_right_title);
        txtView.setText(str);
        txtView.setOnClickListener(clickListener);
    }


    protected void setLeftTitle(String str){
        TextView txtView=(TextView)findViewById(R.id.txt_left_title);
        imgLeft.setVisibility(View.GONE);
        txtView.setVisibility(View.VISIBLE);
        txtView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,MainActivity.class);
                startActivity(intent);
            }
        });
        txtView.setText(str);
    }

    @Override
    public void onClick(View v) {

    }

    protected   void  hideLeft(){

    }

//    protected  void SetMainTemplate(Template tem){
//        if(tem==Template.img_in_center){
//            defaultLayoutId=R.layout.tem_mainlayout1;
//        }
//    }

    public void hideTopBar(){
        findViewById(R.id.rel_top_bar).setVisibility(View.GONE);
    }

    protected void setMainContentView(int resId){
        View view=getLayoutInflater().inflate(resId,null);
        ((FrameLayout)findViewById(R.id.main_content)).addView(view);

    }


    public String getClassName() {
        String clazzName = getClass().getName();
        return clazzName;
    }


    protected  void setBarTitle(String title){
        setTxtViewText(R.id.txt_title,title);
    }


    protected void setTxtViewText(int resId,String text){
        TextView txt=(TextView) findViewById(resId);
        txt.setText(text);
    }

    public void log(String log){
        LogX.trace(TAG,getClassName() + "--" +log);
    }


    @Override
    protected void onPause() {
        LogX.trace(TAG, getClassName() + " - onPause");
        super.onPause();
    }

    @Override
    protected void onResume() {
        LogX.trace(TAG, getClassName() + " - onResume");
        super.onResume();
    }

    @Override
    protected void onStop() {
        LogX.trace(TAG,getClassName()+" - onStop");
        super.onStop();
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        LogX.trace(TAG, getClassName() + " - onRestart");
        super.onRestart();
    }
    @Override
    protected void onDestroy() {
        LogX.trace(TAG, getClassName() + " - onDestroy");
        super.onDestroy();
        dismissLoadingDialog();
    }

    protected void hideInputMethod() {
        InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (im != null && getCurrentFocus() != null) {
            im.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /** 点击空白隐藏软键盘 */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            // 获得当前得到焦点的View，一般情况下就是EditText（特殊情况就是轨迹求或者实体案件会移动焦点）
            View v = getCurrentFocus();

            if (isShouldHideInput(v, ev)) {
                hideSoftInput(v.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘，因为当用户点击EditText时没必要隐藏
     */
    private boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {
                    0, 0
            };
            v.getLocationInWindow(l);
            int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right && event.getY() > top  && event.getY() < bottom) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    /**
     * 多种隐藏软件盘方法的其中一种
     *
     * @param token
     */
    private void hideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    public void showLoadingDialog(){
        showLoadingDialog(true);
    }

    protected LoadingDialog mLoadingDialog;
    public void showLoadingDialog(boolean isCancle){
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(BaseActivity.this);
        }
        mLoadingDialog.setDismissOnTouchOutside(isCancle);

        if (!isFinishing()) {
            mLoadingDialog.show();
        }
    }
    public void dismissLoadingDialog() {
        if (mLoadingDialog != null && !isFinishing()) {
            mLoadingDialog.dismiss();
        }
    }




    public void showToast(String str){
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
//        if(mToast==null){
//            mToast= Toast.makeText(this, str, Toast.LENGTH_SHORT);
//            mToast.setGravity(Gravity.CENTER, 0,300);
//            View view = mToast.getView();
//            view.setPadding(30,20,30,20);
//
//            TextView v = (TextView) mToast.getView().findViewById(android.R.id.message);
//            v.setTextColor(0xFF7c7200);
////            v.setTextSize(25);
//
//            view.setBackgroundColor(getResources().getColor(R.color.main_yellow_color));
//        }else{
//            mToast.setText(str);
//        }
//
//        mToast.show();
    }



}
